package com.haoma.controller;

import com.haoma.annotation.RequestCount;
import com.haoma.dto.MessageDto;
import com.haoma.entity.MessageDb;
import com.haoma.result.Result;
import com.haoma.service.MessageService;
import com.haoma.vo.MessageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Description: 留言接口
 */
@RestController
@RequestMapping("/web/message")
@CrossOrigin

public class MessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 查询所有的留言
     *
     * @return
     */
    @RequestCount
    @GetMapping("/message")
    public Result queryAllMessage() {
        List<MessageVo> messageList = messageService.queryAllMessage();
        return Result.success(messageList);
    }

    /**
     * 添加留言用户
     */
    @RequestCount
    @PostMapping("/save")
    public Result addMessageUser(@RequestBody @Validated MessageDb messageDb) {
        messageService.addMessageUser(messageDb);
        return Result.success();
    }

    /**
     * 留言用户头像
     *
     * @param multipartFile
     * @return
     */
    @RequestCount
    @PostMapping("/fileUpload")
    public Result fileUpload(@RequestParam("file") MultipartFile multipartFile) {
        String fileUrl = messageService.fileUpload(multipartFile);
        return Result.success(fileUrl);
    }


    /**
     * 发送留言信息
     *
     * @param messageDto
     * @return
     */
    @RequestCount
    @PostMapping("/send")
    public Result sendMessage(@RequestBody MessageDto messageDto) {
        messageService.sendMessage(messageDto);
        return Result.success();
    }
}
